#include <QtGui>
#include "toolParts.h"

ToolParts::ToolParts(QWidget *parent) : QTableWidget(parent) {

  curPart=0;

  this->setColumnCount(MAXPARTSITEMS);
  this->setRowCount(3);

  this->setRowHeight(0, 20);
  this->setRowHeight(1, 20);
  this->setRowHeight(2, 20);
  // this->setRowHeight(3, 20);
  
  this->horizontalHeader()->setEnabled(false);
  this->verticalHeader()->setEnabled(false);

  insItem = new QTableWidgetItem("L");
  insItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setVerticalHeaderItem(0, insItem);

  insItem = new QTableWidgetItem("F");
  insItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setVerticalHeaderItem(1, insItem);

  insItem = new QTableWidgetItem("i");
  insItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  this->setVerticalHeaderItem(2, insItem);

  // insItem = new QTableWidgetItem("ir");
  // insItem->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  // this->setVerticalHeaderItem(3, insItem);

  deActiveColor = QColor(220, 220, 220);
  activeColor   = QColor(230, 230, 230);
  // activeColor   = QColor(172, 255, 146);
  // activeColor   = QColor(183, 227, 167);

  resetEdit();

  for (int i = 0; i < MAXPARTSITEMS; i++)
    setColor(deActiveColor, i);

  setEditPart(0);
   
  connect(this, SIGNAL(cellClicked(int, int)), this, SLOT(activePart(int, int)));

} //ToolParts::ToolParts(QWidget *parent = 0) : QTableWidget(parent)

bool ToolParts::isPartNull(int num) {

  if (num > 0)
    
    if (
	(((CellEdit *)this->cellWidget(0, num))->isNull()) &&
	(((CellEdit *)this->cellWidget(1, num))->isNull()) &&
	(((CellEdit *)this->cellWidget(2, num))->isNull())
	// (((CellEdit *)this->cellWidget(3, num))->isNull())
	)

      return true;

    
  return false;

} //bool ToolParts::isPartNull(int num)

int ToolParts::partCount() {
  
  int count = 0;

  for (int i=0; i < MAXPARTSITEMS; i++)
    if (!isPartNull(i))
      count++;

  return count;

} //int ToolParts::partCount()

void ToolParts::setEditPart(int num) {

  if ((isPartNull(curPart)) && (curPart > 0))
    setColor(deActiveColor, curPart);
  else
    setColor(activeColor, curPart);

  editView(false, 0, curPart);
  editView(false, 1, curPart);
  editView(false, 2, curPart);
  // editView(false, 3, curPart);

  editView(true, 0, num);
  editView(true, 1, num);
  editView(true, 2, num);
  // editView(true, 3, num);

  curPart = num;

  emit changedCurPart(curPart);
  emit changeL(((CellEdit *)this->cellWidget(0, curPart))->valueDoubleSpin(), false);
  emit changeF(((CellEdit *)this->cellWidget(1, curPart))->valueDoubleSpin(), false);
  emit changeI(((CellEdit *)this->cellWidget(2, curPart))->valueDoubleSpin(), false);
  emit changeIr(((CellEdit *)this->cellWidget(2, curPart))->valueDoubleSpin(), false);

  setFocusF();

} //void ToolParts::setEditPart(int num)

void ToolParts::editView(bool newVal, int row, int col) {

  insEdit = (CellEdit *)this->cellWidget(row, col);

  if (newVal)
    insEdit->setEditView();
  else
    insEdit->setLabelView();

} //void ToolParts::editView(bool newVal, int row, int col)

void ToolParts::setColor(QColor color, int column) {

  if (column < MAXPARTSITEMS) {

    for (int i=0; i <= 2; i++) {

      insEdit = (CellEdit *)this->cellWidget(i, column);
      insEdit->setColor(color);

    } //for (int i=0; i <= 2; i++)

  } //if (column < MAXPARTSITEMS)

} //void ToolParts::setColor(QColor color, int column)

void ToolParts::activePart(int, int col) {

  setEditPart(col);

} //void ToolParts::activePart(int row, int col)

void ToolParts::setL(double newVal, int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(0, column);
    insEdit->setValueDoubleSpin(newVal);
    emit changeL(newVal);

  } //if (column < MAXPARTSITEMS)

} //void ToolParts::setL(double newVal, int column)

double ToolParts::l(int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(0, column);
    return insEdit->valueDoubleSpin();

  } //if (column < MAXPARTSITEMS)

  return 0;

} //double ToolParts::l(int column)

void ToolParts::setF(double newVal, int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(1, column);
    insEdit->setValueDoubleSpin(newVal);
    emit changeF(newVal);

  } //if (column < MAXPARTSITEMS)

} //void ToolParts::setF(double newVal, int column)

double ToolParts::f(int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(1, column);
    return insEdit->valueDoubleSpin();

  } //if (column < MAXPARTSITEMS)

  return 0;

} //double ToolParts::f(int column)

void ToolParts::setI(double newVal, int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(2, column);
    insEdit->setValueDoubleSpin(newVal);
    emit changeI(newVal);

  } //if (column < MAXPARTSITEMS)

} //void ToolParts::setI(double newVal, int column)

double ToolParts::i(int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(2, column);
    return insEdit->valueDoubleSpin();

  } //if (column < MAXPARTSITEMS)

  return 0;

} //double ToolParts::i(int column)

void ToolParts::setIr(double newVal, int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(2, column);
    insEdit->setValueDoubleSpin(newVal);
    emit changeIr(newVal);

  } //if (column < MAXPARTSITEMS)

} //void ToolParts::setIr(double newVal, int column)

double ToolParts::ir(int column) {

  if (column < MAXPARTSITEMS) {

    insEdit = (CellEdit *)this->cellWidget(2, column);
    return insEdit->valueDoubleSpin();

  } //if (column < MAXPARTSITEMS)

  return 0;

} //double ToolParts::ir(int column)

void ToolParts::genMessageL(double val) {

  QString str;
  str  = "<b>L =</b> ";
  str += QString().setNum(toPrec(val), 'g', STRPREC);
  str += " <i>м</i>";
  emit newMessage(str);

} //void ToolParts::genMessageL(double val)

void ToolParts::genMessageF(double val, double fullFParts) {

  QString str;
  str  = "<b>Водосбор участка</b> ";
  str += QString().setNum(toPrec(val), 'g', STRPREC);
  str += " <i>га</i>";
  str += " (";
  str += QString().setNum(toPrec(val*10000), 'g', STRPREC);;
  str += " <i>м<sup>2</sup></i>)";
  str += ", <b>Общий водосбор</b> ";
  str += QString().setNum(toPrec(fullFParts), 'g', STRPREC);
  str += " <i>га</i>";
  str += " (";
  str += QString().setNum(toPrec(fullFParts*10000), 'g', STRPREC);;
  str += " <i>м<sup>2</sup></i>)";
  emit newMessage(str);

} //void ToolParts::genMessageF(double val, double fullFParts)

void ToolParts::genMessageI(double val) {

  QString str;
  str  = "<b>i =</b> ";
  str += QString().setNum(toPrec(val), 'g', STRPREC);
  emit newMessage(str);

} //void ToolParts::genMessageI(double val)

void ToolParts::genMessageIr(double val) {

  QString str;
  str  = "<b>i<sub>r</sub> =</b> ";
  str += QString().setNum(toPrec(val), 'g', STRPREC);
  emit newMessage(str);

} //void ToolParts::genMessageIr(double val)

void ToolParts::changeL(double val, bool isChange) {

  emit changedL(val);
  if (isChange)
    emit changedValue();
  
  genMessageL(val);

} //void ToolParts::changeL(double val, bool isChange)

void ToolParts::changeF(double val, bool isChange) {

  double fullF = 0;

  for (int num = 0; num <= curPart; num++)
    fullF += ((CellEdit *)this->cellWidget(1, num))->valueDoubleSpin();

  emit changedF(fullF);
  if (isChange)
    emit changedValue();
  
  genMessageF(val, fullF);

} //void ToolParts::changeF(double val, bool isChange)

void ToolParts::setFocusF() {

  double fullF = 0;

  for (int num = 0; num <= curPart; num++)
    fullF += ((CellEdit *)this->cellWidget(1, num))->valueDoubleSpin();

  genMessageF(((CellEdit *)this->cellWidget(1, curPart))->valueDoubleSpin(), fullF);

} //void ToolParts::setFocusF()

void ToolParts::changeI(double val, bool isChange) {

  emit changedI(val);
  if (isChange)
    emit changedValue();
  
  genMessageI(val);

} //void ToolParts::changeI(double val, bool isChange)

void ToolParts::changeIr(double val, bool isChange) {

  emit changedIr(val);
  if (isChange)
    emit changedValue();
  
  genMessageI(val);

} //void ToolParts::changeIr(double val, bool isChange)

void ToolParts::resetEdit() {

  QString toolTip;

  for (int i=0; i < MAXPARTSITEMS; i++) {

    //L
    toolTip = "Длина участка";
    insEdit = new CellEdit(deActiveColor);
    insEdit->setIsShowButton(false);
    insEdit->setIsIntSpin(false);
    insEdit->setDRange(DEFMIN_L, DEFMAX_L);
    insEdit->setStepDSpin(DEFSTEP_L);
    insEdit->setValueDoubleSpin(0.0);
    insEdit->setAddStr(" <i>м</i>");
    insEdit->isIndent(false);
    insEdit->setLabelView();
    insEdit->setToolTip(toolTip);

    connect(insEdit, SIGNAL(changeValueDSpin(double)), this, SLOT(changeL(double)));
    this->setCellWidget(0, i, insEdit);

    //F
    toolTip = "Водосбор участка";
    insEdit = new CellEdit(deActiveColor);
    insEdit->setIsShowButton(false);
    insEdit->setIsIntSpin(false);
    insEdit->setDRange(DEFMIN_F, DEFMAX_F);
    insEdit->setStepDSpin(DEFSTEP_F);
    insEdit->setValueDoubleSpin(0.0);
    insEdit->setAddStr(" <i>га</i>");
    insEdit->isIndent(false);
    insEdit->setLabelView();
    insEdit->setToolTip(toolTip);

    connect(insEdit, SIGNAL(changeValueDSpin(double)), this, SLOT(changeF(double)));
    this->setCellWidget(1, i, insEdit);

    //I
    toolTip = "Уклон участка";
    insEdit = new CellEdit(deActiveColor);
    insEdit->setIsShowButton(false);
    insEdit->setIsIntSpin(false);
    insEdit->setDRange(DEFMIN_I, DEFMAX_I);
    insEdit->setStepDSpin(DEFSTEP_I);
    insEdit->setValueDoubleSpin(0.0);
    insEdit->isIndent(false);
    insEdit->setLabelView();
    insEdit->setToolTip(toolTip);

    connect(insEdit, SIGNAL(changeValueDSpin(double)), this, SLOT(changeI(double)));
    connect(insEdit, SIGNAL(changeValueDSpin(double)), this, SLOT(changeIr(double)));
    this->setCellWidget(2, i, insEdit);

    //Ir
    // toolTip = "Расчетный уклон участка";
    // insEdit = new CellEdit(deActiveColor);
    // insEdit->setIsShowButton(false);
    // insEdit->setIsIntSpin(false);
    // insEdit->setDRange(DEFMIN_I, DEFMAX_I);
    // insEdit->setStepDSpin(DEFSTEP_I);
    // insEdit->setValueDoubleSpin(0.0);
    // insEdit->isIndent(false);
    // insEdit->setLabelView();
    // insEdit->setToolTip(toolTip);

    // connect(insEdit, SIGNAL(changeValueDSpin(double)), this, SLOT(changeIr(double)));
    // this->setCellWidget(3, i, insEdit);

    this->setColumnWidth(i, WCELLEDIT);

  } //for (int i=0; i < MAXPARTSITEMS; i++)

} //void ToolParts::resetEdit()
 
